/*!
// Testweb v0.4.0
// http://or-change.cn
// Copyright 2014 OrChange Inc. All rights reserved.
// Licensed under the GPL License.
*/
/*jslint vars: true, forin: true, plusplus: true */
/*global define: false */
/**
 * Remote Data Interface Management
 * 如果不使用回调模式无法保证异步数据能够顺利
 * dim.query(model_name, param, function(){[statements]});param必须显性用null表示无参数。
 *
 * @module dim
 * @requires jQuery
 * @example
 *     var OC = require("./oc.dim"); //How to require OC module.
 */
define(function (require, exports, module) {
	'use strict';
	var dim, OC = require("lib/core/oc"),
		data_server = OC.cfg("DATA_SERVER");

	window.d = dim = {
		cache: {}
	};

	dim.getAccess = function (param, callback) {
		var url;
		OC.pull(data_server + "get_access/" + param[0] + "/" + param[1] + ".json", function (data) {
			var access = data;
			callback(access);
		});
	};
	dim.listProcess = function (param, callback) {
		if (typeof param === null) {
			return false;
		}
		OC.pull(data_server + "get_process_list/" + param + ".json", function (data) {
			var i, len, list_process = [];
			len = data.length;
			for (i = 0; i < len; i += 1) {
				list_process.push({
					process_id: data[i].processid,
					process_name: data[i].name,
					author: "规划字段",
					updatetime: new Date(data[i].updatetime).toLocaleDateString()
				});
			}
			callback(list_process);
		});
	};
	dim.getProcess = function (param, callback) {
		OC.pull(data_server + "get_process/" + param + ".json", function (data) {
			var process = data;
			callback(process);
		});
	};
	dim.getUser = function (param, callback) {
		OC.pull(data_server + "get_user/" + param + ".json", function (data) {
			var user = data;
			callback(user);
		});
	};
	dim.getProjectInfo = function (param, callback) {
		OC.pull(data_server + "get_project_info/" + param[0] + "/" + param[1] + ".json", function (data) {
			var project_info = data;
			callback(project_info);
		});
	};
	dim.getDictionary = function (param, callback) {
		OC.pull(data_server + "get_dictionary/" + param + ".json", function (data) {
			var dictionary = data;
			callback(dictionary);
		});
	};

	exports.query = function (model_name, param, callback) {
		// 检查model_name是个string。
		if (dim.cache.hasOwnProperty(model_name)) {
			callback(dim.cache[model_name]);
		} else {
			dim[model_name](param, function (data) {
				dim.cache[model_name] = data;
				setTimeout(function () {
					delete dim.cache[model_name];
				}, 10000);
				callback(data);
			});
		}
	};
});